import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_upgrade/src/flutter_upgrade.dart';
import 'package:flutter_upgrade/src/app_market.dart';
import 'package:flutter_upgrade/src/app_upgrade.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('App 升级测试'),
        ),
        body: Stack(
          children: <Widget>[
            Center(
              child: Column(
                children: <Widget>[
                  Home(),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class Home extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  AppInfo _appInfo;
  List<AppMarketInfo> _appMarketList = [];

  @override
  void initState() {
    super.initState();
  }

  _checkAppUpgrade() {
    AppUpgrade.appUpgrade(context, _checkAppInfo(),
        okBackgroundColors: [ Colors.transparent, Colors.transparent],
        cancelText: '残忍拒绝',
        /*cancelTextStyle: TextStyle(
            fontSize: ScreenUtil.getInstance().getSp(15),
            color: HexColor("#999999")),*/
        okText: '立即升级',
        /*okTextStyle: TextStyle(
            fontSize: ScreenUtil.getInstance().getSp(15),
            color: HexColor("#E32416")),*/
        /*titleStyle: TextStyle(
            fontSize: ScreenUtil.getInstance().getSp(15),
            color: HexColor("#999999")),*/
        /*contentStyle: TextStyle(
            fontSize: ScreenUtil.getInstance().getSp(14),
            color: HexColor("#333333")),*/
        progressBarColor: Colors.lightBlue.withOpacity(.4),
        borderRadius: 15,
        iosAppId: 'id88888888',
        appMarketInfo: AppMarket.huaWei);
  }

  AppUpgradeInfo _checkAppInfo() {
    return AppUpgradeInfo(
      title: '有新版本啦！',
      contents: ['1.新增邀请码功能，增加用户活跃度。2.提供政情功能。3.稿件可以添加音频，实现图、文、音乐同步进行。'
      ],
      apkDownloadUrl:
          'https://storage.jd.com/arvrdata/0b7a580a-503b-49b1-8b86-80f0e5467e2f?Expires=1899191583&AccessKey=8Xc003t1kQzVk1eK&Signature=0g%2BxFBfGLejmPjOjogBzTrrORTE%3D',
      force: false,
    );
  }

  _getAppInfo() async {
    var appInfo = await FlutterUpgrade.appInfo;
    setState(() {
      _appInfo = appInfo;
    });
  }

  _getInstallMarket() async {
    List<String> marketList = await FlutterUpgrade.getInstallMarket();
    var packageName = marketList[0];
    AppMarketInfo _marketInfo = AppMarket.getBuildInMarket(packageName);
    print('${_marketInfo.marketName}');
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Text('packageName:${_appInfo?.packageName}'),
        Text('versionName:${_appInfo?.versionName}'),
        Text('versionCode:${_appInfo?.versionCode}'),
        Text('安装的应用商店:${_appMarketList.map((f) {
          return f.marketName;
        }).toList()}'),
        RaisedButton(onPressed: (){
          _checkAppUpgrade();
//          _getInstallMarket();
        },child: Text("升级apk"),)
      ],
    );
  }
}
